package com.lanou3g.demo.base.controller;

import com.lanou3g.demo.base.service.IAccountService;
import com.lanou3g.demo.entity.Account;
import com.lanou3g.demo.entity.ExAccount;
import com.lanou3g.demo.validate.AddAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sound.midi.Soundbank;
import java.awt.print.Pageable;
import java.lang.reflect.Field;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@Controller
public class AccountController {
    @Autowired
    private IAccountService accountService;

    @RequestMapping("/account")
    public String view() {
        return "/account/account_list";
    }

    @RequestMapping("/account.page")
    public String findByTerm(Model model, ExAccount account) {
        if (account.getStatus() == null) {
            account.setStatus("0");
        }
        if (account.getPage() == null || account.getPage().toString().equals("0")) {
            account.setPage(1);
        }
        // 获取当前页 非0情况下
        Integer currentPage = account.getPage();
        // 获取状态 传给前端
        String status = account.getStatus();
        model.addAttribute("status", status);
        // 设置页数 公式 (page-1) * 3
        account.setPage((account.getPage() - 1) * 3);
        List<ExAccount> accountList = accountService.findByTerm(account);
        model.addAttribute("accountList", accountList);
        List<ExAccount> totalLiST = accountService.findAllAccount(account);
        for (ExAccount exAccount : totalLiST) {
            Integer total = exAccount.getTotal();
            int totalPage = total / 3;
            if (total >= 0 && total < 3) {
                totalPage = 1;
            } else {
                if ((total % 3) >= 1) {
                    totalPage += 1;
                }
            }
            model.addAttribute("totalPage", totalPage);
            if (currentPage >= totalPage) {
                currentPage = totalPage - 1;
            }
            model.addAttribute("currentPage", currentPage);
        }
        return view();
    }

    @RequestMapping("/status.do")
    public String modifyStatus(Account a) {
        Account account = new Account();
        Integer accountId = a.getAccountId();
        String status = a.getStatus();
        account.setAccountId(accountId);
        account.setStatus(status);
        accountService.modifyStatus(account);
        if (!status.equals("1")) {
            accountService.modifyServiceStatus(account);
        }
        return "redirect:/account.page";
    }

    @RequestMapping("/account.detail")
    public String findDetail(Integer accountId, Model model) {
        ExAccount account = accountService.findDetail(accountId);
        if (account.getRecommenderId() != null) {
            ExAccount exAccount = accountService.findRecommenderIdCard(account);
            model.addAttribute("account", exAccount);
        } else model.addAttribute("account", account);
        return "/account/account_detail";
    }

    @RequestMapping("/account.add")
    public String addAccount(@Validated(AddAccount.class) ExAccount account, BindingResult result, Model model) {
        if (result.hasErrors()) {
            model.addAttribute("result", result);
            return "/account/account_add";
        } else {
            Map<String, Integer> map = accountService.findByIdcardNo(account.getIdcardNo());
            Integer id = map.get("id");
            if (!id.toString().equals("")) {
                model.addAttribute("repeat", "此身份证已被注册");
                return "/account/account_add";
            }
            account.setStatus("1");
            accountService.addAccount(account);
            return "redirect:/account.page";
        }
    }

    @RequestMapping("/account.modifyInfo")
    public String modifyInfo(@Validated(AddAccount.class) ExAccount account, BindingResult result, Model model) {
        if (result.hasErrors()) {
            model.addAttribute("result", result);
            ExAccount detail = accountService.findDetail(account.getAccountId());
            model.addAttribute("account", detail);
            return "/account/account_modi";
        } else {
            accountService.modifyInfo(account);
            ExAccount detail = accountService.findDetail(account.getAccountId());
            model.addAttribute("account", detail);
            return "/account/account_modi";
        }
    }
}
